package gold.digger;

import gold.utils.InputUtil;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1418 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<List<String>> displayTable(List<List<String>> orders) {
            Map<Integer, Map<String, Integer>> screenMap = new TreeMap<>();

            Set<String> titleSet = new TreeSet<>();
            for (List<String> order : orders) {
                int table = Integer.parseInt(order.get(1));
                String meal = order.get(2);
                if (!titleSet.contains(meal)) titleSet.add(meal);

                Map<String, Integer> tableMealCount;
                if (screenMap.containsKey(table)) tableMealCount = screenMap.get(table);
                else tableMealCount = new TreeMap<>();
                tableMealCount.put(meal, tableMealCount.getOrDefault(meal, 0) + 1);
                screenMap.put(table, tableMealCount);
            }

            List<List<String>> screen = new ArrayList<>();
            List<String> title = new ArrayList<>(Arrays.asList("Table"));
            title.addAll(titleSet);
            screen.add(title);

            for (int table : screenMap.keySet()) {
                Map<String, Integer> tableMenu = screenMap.get(table);
                List<String> line = new ArrayList<>(Arrays.asList(String.valueOf(table)));
                for (String meal : titleSet) {
                    if (tableMenu.containsKey(meal)) line.add(String.valueOf(tableMenu.get(meal)));
                    else line.add("0");
                }
                screen.add(line);
            }

            return screen;
        }

    }

    public void run() {
        Solution solution = new Solution();
        List<List<String>> orders = InputUtil.toDoubleStringList("[[\"David\",\"3\",\"Ceviche\"],[\"Corina\",\"10\",\"Beef Burrito\"],[\"David\",\"3\",\"Fried Chicken\"],[\"Carla\",\"5\",\"Water\"],[\"Carla\",\"5\",\"Ceviche\"],[\"Rous\",\"3\",\"Ceviche\"]]");
        System.out.println(solution.displayTable(orders));
    }

    public static void main(String[] args) throws Exception {
        LC1418 an = new LC1418();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
//Input: orders = [["David","3","Ceviche"],["Corina","10","Beef Burrito"],["Davi
//d","3","Fried Chicken"],["Carla","5","Water"],["Carla","5","Ceviche"],["Rous","3
//","Ceviche"]]
//Output: [["Table","Beef Burrito","Ceviche","Fried Chicken","Water"],["3","0","
//2","1","0"],["5","0","1","0","1"],["10","1","0","0","0"]]
//Explanation:
//The displaying table looks like:
//Table,Beef Burrito,Ceviche,Fried Chicken,Water
//3    ,0           ,2      ,1            ,0
//5    ,0           ,1      ,0            ,1
//10   ,1           ,0      ,0            ,0
//For the table 3: David orders "Ceviche" and "Fried Chicken", and Rous orders "
//Ceviche".
//For the table 5: Carla orders "Water" and "Ceviche".
//For the table 10: Corina orders "Beef Burrito".
//
//